package com.star.bookstore.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.bookstore.common.utils.PageUtils;
import com.star.bookstore.common.utils.Query;
import com.star.bookstore.product.dao.CommentDao;
import com.star.bookstore.product.entity.CommentEntity;
import com.star.bookstore.product.service.CommentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, CommentEntity> implements CommentService {

    @Autowired
    CommentDao commentDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        QueryWrapper<CommentEntity> wrapper = new QueryWrapper<CommentEntity>();
        if (!"".equals(key)){
            wrapper.and(obj->{
                obj.eq("com_username",key).or().like("com_message",key);
            });
        }

        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(params),
                wrapper);

        List<CommentEntity> commentList = new ArrayList<>();
        //将日期格式化
        page.getRecords().forEach(commentEntity -> {
            Date createTime = commentEntity.getCreateTime();
            String createTimeStr = new SimpleDateFormat("yyyy年 MM月 dd日").format(createTime);
            commentEntity.setCreateTimeStr(createTimeStr);
            commentList.add(commentEntity);
        });
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(commentList);

        return pageUtils;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {

        IPage<CommentEntity> page ;

        if (categoryId == 0){
            /**
             * page(分页信息对象（页数，每页记录数），查询条件)
             */
            page = this.page(
                    new Query<CommentEntity>().getPage(params),
                    new QueryWrapper<>());
        }else{
            String key = (String) params.get("key");
            QueryWrapper<CommentEntity> wrapper = new QueryWrapper<CommentEntity>().eq("category_id", categoryId);

            if (!StringUtils.isEmpty(key)){
                wrapper.and(obj->{
                    obj.eq("com_username",key).or().like("com_message",key);
                });
            }
            page = this.page(
                    new Query<CommentEntity>().getPage(params),
                    wrapper);
        }

        List<CommentEntity> commentList = new ArrayList<>();
        page.getRecords().forEach(commentEntity -> {
            Date createTime = commentEntity.getCreateTime();
            String createTimeStr = new SimpleDateFormat("yyyy年 MM月 dd日").format(createTime);
            commentEntity.setCreateTimeStr(createTimeStr);
            commentList.add(commentEntity);
        });
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(commentList);

        return pageUtils;

    }

}